{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da217181-ed5c-49f1-80df-d3af53fe239e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly\n",
    "import plotly.express as px\n",
    "import pandas as pd\n",
    "\n",
    "pic_name = 'Pyecharts-render.html'\n",
    "plt = plotly.offline.plot\n",
    "df = pd.read_csv('601225.csv').head(200)\n",
    "df = df[['日期', '开盘', '收盘', '涨跌额']]\n",
    "fig = px.line(df, x='日期', y=['开盘', '收盘']) # 多条线 \n",
    "plt(fig, filename=pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c73373f1-ffc4-4c6f-963f-f431ea0b9f34",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.scatter(df, x='日期', y='涨跌额')  # 使用数据列可指定color, size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cec0eea-e525-4112-ae9e-75665fec2e44",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['星期'] = pd.to_datetime(df['日期'], format='%Y-%m-%d').dt.weekday\n",
    "px.line(df.head(50), x='日期', y='开盘', color='星期', line_group='星期', facet_col='星期')  # facet_col 分组并排在一行多列, facet_row 一列多行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8ba6f73-b27e-4de1-8788-326de4f76680",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.area(df[:50], x='日期', y='涨跌额')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68ad906e-6b70-4efd-9071-75554f770545",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.scatter_matrix(df[:50], dimensions=['开盘', '收盘'])  # 散点图矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a9c9b9-c482-45fe-9e64-6b1a5e8dc406",
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = df[:100].copy()\n",
    "pf['开盘'] = pf['开盘'].abs()\n",
    "kp = pf['开盘']\n",
    "jj = kp.mean()\n",
    "# jj, kp.min() - 1, kp.max()+1   # 2.9226, 1.19, 4.1\n",
    "pf['price'] = pd.cut(kp, bins=[kp.min() - 1.5, 2, 2.5, 3, kp.max() + 1], labels=['底价', '低价', '震荡', '溢价'])\n",
    "px.pie(pf, values='开盘', names='price',)  # hole 设置中间空心半径与外圆半径的比例 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6079c4a6-f3f7-4089-8560-3e29b1b6ebb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.bar(pf[:50], x='日期', y='开盘', color='price', orientation='h') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a993ad1f-627c-4330-869f-529724eaf22e",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.bar(pf[:50], x='日期', y='开盘', color='price')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a6f07bf-7d10-47a3-81b9-d75f6a0bdc36",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.box(pf[:50], x='星期', y='开盘')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11715918-e438-4e6e-9e74-24089dbc15d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.violin(pf[:50], x='星期', y='开盘', box=True, points='all')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f5d3732-099a-42e8-819b-949672018e10",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.histogram(df, x='收盘') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc244baa-c9d0-4d3b-b55e-8717df5c701b",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.funnel(df[:10], x='日期', y='price')  # x递减数据列, y分层级的名称列\n",
    "# px.funnel_area(df[:10], names='日期', values='开盘')   # 未验证 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd0e4014-7912-4dbb-8792-194dd69e7fc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 极坐标图: \n",
    "\n",
    "categories = pd.Series(['A', 'B', 'C', 'D'])\n",
    "values = pd.Series([10, 20, 15, 25])\n",
    "data = pd.DataFrame(data=(categories, values))\n",
    "print(data)\n",
    "px.bar_polar(r=values, theta=categories, color_discrete_sequence=px.colors.sequential.Blugrn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "402603fb-3873-46a0-8ad2-e77f236b394d",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.scatter_polar(r=values, theta=categories, color_discrete_sequence=px.colors.sequential.Blugrn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe83bb99-3a82-426d-bec2-cf0ff18572b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "px.line_polar(r=values, theta=categories, color_discrete_sequence=px.colors.sequential.Blugrn)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
